package bjut.core.loadbalance;

import bjut.core.Message.RpcRequest;
import bjut.core.interfaces.LoadBalance;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * @Author: zhuzhiming
 * @Date: 2022/4/21 18:44
 */
public abstract class LoadBalanceAbstract implements LoadBalance {

    @Override
    public String selectService(List<String> serviceAddresses, RpcRequest rpcRequest) {
        if (CollectionUtils.isEmpty(serviceAddresses)) {
            return null;
        }
        if (serviceAddresses.size() == 1) {
            return serviceAddresses.get(0);
        }
        return doSelect(serviceAddresses, rpcRequest);
    }

    protected abstract String doSelect(List<String> serviceAddresses, RpcRequest rpcRequest);

}
